%% SOLVES INFORMATION RENT IN LIMITED-COMMITMENT CONTRACT
function [V] = limited_inforent_solver(C, Beta, Eta, params, grid_params)
%% PARAMTERS
temp = num2cell(params);
[rho, r, ra, alpha, sigma, lambda, M, eta_min, eta_max] = temp{:};

[n_grid, ~, PHI_mat, ~, dphi] = grid_params{:};

PHI = PHI_mat(1,:)';


%% v-term
mat_v = -C.^(1-rho);

AA_v = spdiags(mat_v, 0, n_grid,n_grid);

%% AA matrix for v-term and first derivatives
AA = AA_v;

%% Second derivatives
vol_phi = Eta.*PHI.*(1-PHI);
vol2_phi = vol_phi.^2/(2*dphi^2);

up_mat_vol_phi = [0;vol2_phi];

cent_mat_vol_phi = -vol2_phi*2;

low_mat_vol_phi = [vol2_phi(2:end);0];

BB_phiphi =spdiags(cent_mat_vol_phi,0,n_grid,n_grid)+...
    spdiags(low_mat_vol_phi,-1,n_grid,n_grid)+...
    spdiags(up_mat_vol_phi,1,n_grid,n_grid);

BB = BB_phiphi;

%% "Cross derivatives" with Scaling Variable
cross_phi = (1-rho)*Beta.*vol_phi/(dphi);
up_cross_phi = max(0,cross_phi);
cent_cross_phi = - max(0,cross_phi) - max(0,-cross_phi);
low_cross_phi = max(0,-cross_phi);

up_mat_cross_phi = [0;up_cross_phi];
cent_mat_cross_phi = cent_cross_phi;
low_mat_cross_phi = [low_cross_phi(2:end);0];

CC_phi = spdiags(up_mat_cross_phi,1,n_grid,n_grid)+...
         spdiags(cent_mat_cross_phi, 0, n_grid, n_grid)+...
         spdiags(low_mat_cross_phi,-1,n_grid,n_grid);    

CC = CC_phi;

%% Flow
f = Beta.*Eta.*PHI.*(1-PHI);

%% New value function
P = AA + BB + CC;

Q = (- P);

V = Q\(f);
end
